package com.bjpowernode.controller.advice;


import com.bjpowernode.dto.ResultDTO;
import com.bjpowernode.map.MySQLErrorMap;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;

@RestControllerAdvice
public class SQLExceptionAdviceController {

    @ExceptionHandler(SQLException.class)
    public ResultDTO sqlException(SQLException e, HttpServletRequest request){
        //请求地址
        String requestURI = request.getRequestURI();
        ResultDTO resultDTO=new ResultDTO();
        resultDTO.setResult(false);
        String mess="";
        //唯一性约束处理
        if ((e.getMessage().indexOf("Duplicate entry") != -1) && (e.getMessage().indexOf("for key 'name'") != -1)) {
            mess = MySQLErrorMap.SQLDuplicateMap.get(requestURI+"#name");
        }
        resultDTO.setMess(mess);
        return resultDTO;


    }


}
